package com.thon.controller.student;

import com.thon.controller.util.BaseController;
import com.thon.entity.topic.Choice;
import com.thon.entity.topic.Topic;
import com.thon.service.system.SystemService;
import com.thon.service.topic.ChoiceService;
import com.thon.service.topic.TopicService;
import com.thon.service.utils.UserUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by Chen on 2016/2/28.
 */
@Controller
@RequestMapping(value = "/student")
public class StudentController extends BaseController {

    @Autowired
    private SystemService systemService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private ChoiceService choiceService;

    @RequestMapping(method = RequestMethod.GET)
    public String student() {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }

        return "student";
    }

    @RequestMapping(value="/user/profile", method = RequestMethod.GET)
    public String adminProfile(Model model) {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }

        int userId = getCurrentUserId();
        model.addAttribute("user", systemService.getUser(userId));
        return "student-user-profile";
    }

    @RequestMapping(value="/topic/manage", method = RequestMethod.GET)
    public String topicManage() {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
        return "student-topic-manage";
    }

    @RequestMapping(value="/topic/form", method = RequestMethod.GET)
    public String topicForm(@RequestParam(value="id",required=true) Integer id, Model model) {

        Topic topic = topicService.getTopic(id);

        model.addAttribute("topic", topic);
        return "student-topic-form";
    }

    @RequestMapping(value="/choice/manage", method = RequestMethod.GET)
    public String choiceManage() {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
        return "student-choice-manage";
    }

    @RequestMapping(value="/choice/form", method = RequestMethod.GET)
    public String choiceForm(@RequestParam(value="id",required=true) Integer id, Model model) {

        Choice choice = choiceService.getChoice(id);

        model.addAttribute("choice", choice);
        return "student-choice-form";
    }

    @RequestMapping(value="/topic/mine", method = RequestMethod.GET)
    public String topicMine() {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
        return "student-topic-mine";
    }

    @RequestMapping(value="/topic/add/form", method = RequestMethod.GET)
    public String topicAddForm(@RequestParam(value="id",required=false) Integer id, Model model) {

        Topic topic = new Topic();
        if (id != null) {
            topic = topicService.getTopic(id);
        }
        model.addAttribute("topic", topic);
        return "student-topic-add-form";
    }

    @RequestMapping(value="/user/schedule", method = RequestMethod.GET)
    public String lookSchedule(Model model) {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
//        int userId = getCurrentUserId();
//        model.addAttribute("user", systemService.getUser(userId));
        return "student-user-schedule";
    }

    @RequestMapping(value="/paper/upload", method = RequestMethod.GET)
    public String paperUpload() {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
        int studentId = UserUtils.getUser().getId();
        if (systemService.hasRelation(studentId)) {
            return "student-paper-upload";
        } else {
            return "student-paper-upload-error";
        }
    }

    @RequestMapping(value="/user/password", method = RequestMethod.GET)
    public String editPassword() {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
        return "student-user-password";
    }
}
